<?php
namespace app\api\controller;

use app\api\model\User as UserModel;
use think\Db;

header('Access-Control-Allow-Origin:*');
// 响应类型
header('Access-Control-Allow-Methods:*');
// 响应头设置
header('Access-Control-Allow-Headers:x-requested-with,content-type');

class Login
{

    public function send()
    {

        ini_set("error_reporting", "E_ALL & ~E_NOTICE");

        $obj->gameID = '129';
        $obj->pageSize = '1000';
        $obj->pageIndex = '1';
        $obj->_ = '1539321037216';
        $data = json_encode($obj);
        $url = "https://9c0022.com/Result/GetLotteryResultList?gameID=129&pageSize=301&pageIndex=1&_=1539321037216";
        $res = $this->http_request($url, $data);

        return json(['json' => $res]);
    }

    public function getyuce()
    {

        if ($_SERVER['REQUEST_METHOD'] == 'OPTIONS') {
            header("Access-Control-Allow-Origin: *");
            header("Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept, Authorization");
            header('Access-Control-Allow-Methods: GET, POST, PUT,DELETE,OPTIONS,PATCH');
            return;
        }

        $result1 = Db::table('yucenumber')
            ->where('yuce', '>', -1)
            ->select(); // 批量添加

        return $result1;

    }

    public function GetData()
    {

        if ($_SERVER['REQUEST_METHOD'] == 'OPTIONS') {
            header("Access-Control-Allow-Origin: *");
            header("Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept, Authorization");
            header('Access-Control-Allow-Methods: GET, POST, PUT,DELETE,OPTIONS,PATCH');
            return;
        }

        $result = "";
        $content = $_GET['telValue'];
        $state = $_GET['state'];

        if ($state == 1) {

            $result = Db::table('txffc') //指定tp5_staff表
                ->where('yuce', '>', -1)
                ->select(); // 批量添加
        } else {
//                $result = Db::table('txffc')  //指定tp5_staff表
            //                 ->where('result', 'like', '%'.$content.'%')
            //                ->select();    // 批量添加

            $result = Db::table('txffc') //指定tp5_staff表
                ->where('yuce', '>', -1)
                ->select(); // 批量添加
        }

        return $result;

    }

// "id": 270623551,
    // "period": 201811170673,
    // "date": "2018-11-17 11:12:59",
    // "result": "9,2,3,4,8",
    // "year": 2018

// HTTP请求（支持HTTP/HTTPS，支持GET/POST）
    public function http_request($url, $data = null)
    {
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
        if (!empty($data)) {
            curl_setopt($curl, CURLOPT_POST, 1);
            curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
        }
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        $output = curl_exec($curl);
        curl_close($curl);
        return $output;
    }

    public function add()
    {

        $user = new UserModel;
        if (!$_POST) {

            $msg = "搞事情阿";
            return $msg;
        } else {

            $user['BeautyTitle'] = $_POST['BeautyTitle'];
            $user['Beautyper'] = $_POST['Beautyper'];
            $user['BeautyTime'] = $_POST['BeautyTime'];
            $user['BeautyType'] = $_POST['BeautyType'];
            $user['BeautyContent'] = $_POST['BeautyContent'];
            $user['id'] = 1;
            if ($user->save()) {

                return $res = array(
                    'code' => '200',
                    'msg' => '添加成功',
                );

            } else {
                return $res = array(
                    'code' => '201',
                    'msg' => '添加失败',
                );
            }
        }
    }

    public function Login()
    {

        if (!($_POST)) {

            return json([
                'code' => 400,
                'msg' => '请求错误,请使用POST',
            ]);
        } else {

            $user = new UserModel;
            $username = $_POST['username'];
            $password = $_POST['password'];
            $result = Db::name('user')
                ->where('userid', $username)
                ->select();

            if (count($result) > 0) {
                if ($result[0]['password'] == $password) {
                    if ($result[0]['userid'] != 'admin') {

                        $sysroleVoList = Db::table('sys_role') //指定tp5_staff表
                            ->where('roleid', '=', $result[0]['roleid'])
                            ->select(); //
                        $roleids = explode(',', $sysroleVoList[0]['menuid']);

                        $arr = Db::table('sys_menu')->where('menuid', 'in', $roleids)
                            ->select();

                        $refer = array();
                        $tree = array();
                        foreach ($arr as $k => $v) {
                            $refer[$v['menuid']] = &$arr[$k]; //创建主键的数组引用
                        }

                        foreach ($arr as $k => $v) {
                            $pid = $v['parentid']; //获取当前分类的父级id
                            if ($pid == 0) {
                                $tree[] = &$arr[$k]; //顶级栏目
                            } else {
                                if (isset($refer[$pid])) {
                                    $refer[$pid]['children'][] = &$arr[$k]; //如果存在父级栏目，则添加进父级栏目的子栏目数组中
                                }
                            }
                        }

                    } else {
                        $tree = [];
                    }
                    // 获取菜单

                    return json([
                        'msg' => '成功',
                        'code' => 200,
                        'data' => $result,
                        'sysMenuVoList' => $tree,
                    ]);
                } else {

                    return json([
                        'code' => 401,
                        'msg' => '用户名和密码错误',
                    ]);
                }
            }

        }

    }

}
